import React, { createContext, useContext } from 'react';

interface PositionContextType {
  pos: number[];
  setPos: React.Dispatch<React.SetStateAction<number[]>>;
}

const PositionContext = createContext<PositionContextType | undefined>(undefined);

export const PositionProvider: React.FC<{
  children: React.ReactNode;
  defaultPos?: number[]; // 可选的默认位置
}> = ({ children, defaultPos = [113.809225, 34.800263] }) => {
  // 保留原有的数组形式状态定义
  const [pos, setPos] = React.useState<number[]>(defaultPos);

  return (
    <PositionContext.Provider value={{ pos, setPos }}>
      {children}
    </PositionContext.Provider>
  );
};

export const usePosition = () => {
  const context = useContext(PositionContext);
  if (!context) {
    throw new Error('usePosition must be used within a PositionProvider');
  }
  return context;
};